import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

const fetchHome = (cycle, date, showdate, carid) => (dispatch) => {
  Request.get({
    url: `/dealer/dashboard${cycle || ''}`,
    success: (res) => {
      dispatch(
        receiveHomeList(
          res.users_stat,
          res.stats,
          res.cars,
          date,
          showdate,
          carid,
          true,
          res.total_user_num,
        ),
      );
    },
    fail: (res) => {
      console.log('res1', res);
      dispatch(receiveHomeList([], {}, {}, '', '', '', true, ''));
    },
    error: (error) => {
      console.log('res2', error);
      dispatch(receiveHomeList([], {}, {}, '', '', '', false, ''));
    },
  });
};

const receiveHomeList = (Home, Carousel, CarModel, date, showdate, carid, request, total) => ({
  type: types.RECEIVE_HOME,
  Home,
  Carousel,
  CarModel,
  date,
  showdate,
  carid,
  request,
  total,
});

export default fetchHome;
